package org.jiangwei.cmput696.entitylinking;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ELQueryParser {

	public List<ELQuery> parse(String filePath) throws QueryParsingException {
		NodeList nodeLst = Helper.extractXMLNodeList(new File(filePath), "query");

		List<ELQuery> result = new ArrayList<ELQuery>();

		for (int s = 0; s < nodeLst.getLength(); s++) {
			Element elementNode = (Element) nodeLst.item(s);

			String queryId = elementNode.getAttribute("id");
			String name, docId, begStr, endStr, expected;
			try {
				name = Helper.readNodeChild(elementNode, "name");
				docId = Helper.readNodeChild(elementNode, "docid");
				begStr = Helper.readNodeChild(elementNode, "beg");
				endStr = Helper.readNodeChild(elementNode, "end");
				expected = Helper.readNodeChild(elementNode, "entity");
			} catch (Exception e) {
				throw new QueryParsingException(
						"Error occured when parsing query '" + queryId + "'.",
						e);
			}

			ELQuery query = new ELQuery();
			query.setDocId(docId);
			try {
				query.setBegin(Integer.parseInt(begStr));
				query.setEnd(Integer.parseInt(endStr) + 1);
			} catch (NumberFormatException e) {
				throw new QueryParsingException(
						"Failed to parse offsets for query '" + queryId + "'.",
						e);
			}
			query.setId(queryId);
			query.setSurface(name);
			query.setDocument(createQueriedDocument(docId, query.getBegin(), query.getEnd()));
			
			System.out.println(queryId + " " + expected);

			result.add(query);
		}

		return result;
	}

	private static ELQueriedDocument createQueriedDocument(String docId, int begin, int end)
			throws QueryParsingException {
		String docPath = getPath(docId);
		return docPath == null ? null : new ELQueriedDocument(docPath, begin, end);
	}

	private static String getPath(String docid) {
		String docLoc = "/largedata1/cmput696/TAC_2010_KBP_Source_Data/data/";
		String prefix1 = new String(docLoc + "2009/nw");
		String prefix2 = new String(docLoc + "2010/wb");
		String prefix3 = new String(docLoc + "2009/wb");

		String path = null;
		if (docid.startsWith("eng")) {
			path = prefix2 + "/" + docid + ".sgm";
		} else if (docid.contains(".com_")) {
			path = prefix3 + "/" + docid + ".sgm";
		} else if (docid.startsWith("cmn")) {
			path = null;
		} else if (docid.contains("_CMN_20")) {
			path = null;
		} else {
			String toks1[] = docid.toLowerCase().split("_");
			String toks2[] = toks1[2].split("\\.");

			path = prefix1 + "/" + toks1[0] + "_" + toks1[1] + "/" + toks2[0]
					+ "/" + docid + ".sgm";
		}

		if (path == null)
			return null;

		// File f = new File(path);
		// if (f.exists())
		// return path;

		return path;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println(getPath("eng-WL-110-174612-12992627"));
	
//		String filePath = "test_files/data/tac12-queries-dev.xml";
//		ELQueryParser queryParser = new ELQueryParser();
//		List<ELQuery> queries = null;
//		try {
//			queries = queryParser.parse(filePath);
//		} catch (QueryParsingException e) {
//			e.printStackTrace();
//		}
//		if (queries != null) {
//			for (ELQuery query : queries) {
//				// System.out.println("Query '" + query.getId() + "'");
//				// System.out.println("Name '" + query.getSurface() + "'");
//				// System.out.println("DocID: " + query.getDocId());
//				// System.out.println("Offset: (" + query.getBegin() + ", "
//				// + query.getEnd() + ")");
//				System.out.println(query.getDocument().getDocPath());
//				// System.out.println();
//			}
//			System.out.println(queries.size() + " queries.");
//		}
	}

}
